<template>
  <div>
          <!-- 搜索栏 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="分类名称">
          <el-input v-model="formInline.type" placeholder="请输入分类"></el-input>
        </el-form-item>
        <!-- 开始时间 -->
        <el-form-item class="block">
          <span class="demonstration">创建时间</span>
          <el-date-picker
            v-model="formInline.begin"
            type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
    <!-- 结束时间 -->
        <el-form-item class="block">
          <span class="demonstration">创建时间</span>
          <el-date-picker
            v-model="formInline.end"
            type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="el-icon-search">查询</el-button>
          <el-button type="primary" @click="addCategoryWindow = true" class="addButton el-icon-plus">新增图书</el-button>
        </el-form-item>
    </el-form>

    <!-- 主体展示 -->
    <el-table :data="categoryList" style="width: 100%" border>
      <el-table-column prop="type" label="图书类别" width="250"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="250"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="250"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row)" class="el-icon-delete" size="mini">删除</el-button>
          <el-button type="primary" @click="openUpdateWindow(scope.row)" class="el-icon-edit" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
        <!-- 分页条 -->
  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 8, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>

  <!-- 新增分类窗口 -->
  <el-dialog title="收货地址" :visible.sync="addCategoryWindow">
  <el-form :model="addCategoryDate">
    <el-form-item label="活动名称">
      <el-input v-model="addCategoryDate.type" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="addCategoryWindow = false">取 消</el-button>
    <el-button type="primary" @click="addCategory()">确 定</el-button>
  </div>
</el-dialog>

  <!-- 修改分类窗口 -->
  <el-dialog title="收货地址" :visible.sync="updateCategoryWindow">
  <el-form :model="updateCategoryDate">
    <el-form-item label="活动名称">
      <el-input v-model="updateCategoryDate.type" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="updateCategoryWindow = false">取 消</el-button>
    <el-button type="primary" @click="updateCategory()">确 定</el-button>
  </div>
</el-dialog>

  </div>
</template>

<script>
import { getAllCategoryPage, deleteCategoryById, addCategory, getCategoryById, updateCategoryById } from '@/api/category.js'
export default {
  name: 'CategoryPage',
  data () {
    return {
      categoryList: [], // 分页查询返回的数据
      total: null, // 分页查询返回的总数
      page: 1,
      pageSize: 5,
      formInline: {
        begin: '',
        end: '',
        type: ''
      },
      addCategoryWindow: false, // 新增分类窗口
      updateCategoryWindow: false, // 修改分类窗口
      addCategoryDate: { // 新增的数据存在这里
        type: ''
      },
      updateCategoryDate: {
        id: null,
        type: ''
      }
    }
  },
  async created () {
    this.getAllCategoryPage()
  },
  methods: {
    // 打开编辑窗口
    async openUpdateWindow (row) {
      const res = await getCategoryById(row.id)
      this.updateCategoryDate = res.data
      this.updateCategoryWindow = true
    },
    // 修改图书分类
    async updateCategory () {
      await updateCategoryById(this.updateCategoryDate.id, this.updateCategoryDate.type)// 发送修改请求
      this.getAllCategoryPage()
      this.updateCategoryWindow = false
      this.$message({
        type: 'success',
        message: '修改成功'
      })
    },
    // 新增图书分类
    async addCategory () {
      this.addCategoryWindow = true
      await addCategory(this.addCategoryDate.type)
      this.getAllCategoryPage()
      this.addCategoryDate = {}
      this.addCategoryWindow = false
    },
    // 删除图书分类
    del (row) {
      console.log(row.id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteCategoryById(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getAllCategoryPage()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    // 分页查询
    async getAllCategoryPage () {
      const res = await getAllCategoryPage(this.page, this.pageSize, this.formInline.type, this.formInline.begin, this.formInline.end)
      this.categoryList = res.data.records
      this.total = res.data.total
    },
    // 每页条数改变时触发
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.getAllCategoryPage()
    },

    // 当前页码改变时触发
    handleCurrentChange (page) {
      this.page = page
      // 调用分页查询
      this.getAllCategoryPage()
    },
    // 点击查询按钮
    onSubmit () {
      this.getAllCategoryPage()
    }
  }
}
</script>

<style>

</style>
